package aishua.com.mytestapplist.engin.impl;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.List;

import aishua.com.mytestapplist.engin.WebServiceConstant;
import aishua.com.mytestapplist.utils.CTWalletLog;
import aishua.com.mytestapplist.utils.Convert;
import aishua.com.mytestapplist.utils.Converter;


public class WebConnection extends WebServiceImpl {
	private String host;
	public WebConnection(String host){
		this.host = host;
	}
	@Override
	public <Result, Param> Result executePost(Param param,Class<Result> clazz) {
		try {
			String params = "ClientReq="+ Convert.convertBean2Str(param);
			InputStream outStream = executeHttpsPost(host, params);
			Result result = Convert.getBean(clazz, Object.class, outStream);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public <Result, Param, Inside> Result exectePost(Param param, Class<Result> outSideClazz, Class<Inside> inSideClazz){
		try{
			String params = "ClientReq="+Convert.convertBean2Str(param);
			CTWalletLog.printW("request--->"+params);
//			String result = executeHttpsPost(host, params, true);
//			System.out.println("result---->"+result);
			InputStream inStream = executeHttpsPost(host, params);
			Result t = Convert.getBean(outSideClazz, inSideClazz, inStream);
			CTWalletLog.printD("response--->"+t.toString());;
			return t;
		}catch(Exception e){
			return null;
		}

	}

	public <Result, Param, Inside> Object execteGet(Param param,
													Class<Result> outSideClazz, Class<Inside> inSideClazz) {
		
		int status  = WebServiceConstant.PARSER_SUCCESS;
		try {
			String params = "ClientReq=" + Converter.convertBean2Str(param);
			CTWalletLog.printW("request--->" + params);
//			CTWalletLog.printW("response--->" + executeHttpsPost(host, params, true));
			InputStream inStream = executeHttpsPost(host, params);
			Result t = Converter.getBean(outSideClazz, inSideClazz,
					inStream);
			CTWalletLog.printW("response--->" + t.toString());
//			t.setCurrentState(WebServiceConstant.PARSER_SUCCESS);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
			if (e.getClass().equals(FileNotFoundException.class)) {
				status = WebServiceConstant.EXCEPTION_SERVER_ERROR;
			} else if (e.getClass().equals(ConnectException.class)) {
				status = WebServiceConstant.EXCEPTION_CONNECT_FAILED;
			} else {
				status = WebServiceConstant.EXCEPTION_TIME_OUT;
			}
		}
		return status;

	}
	
	public <Outer, Param> Object exectePost(Param param, Class<Outer> outerClazz,
											List<Class<?>> insides) {
		int status  =WebServiceConstant.PARSER_SUCCESS;
		try {
			String params = "ClientReq="+Converter.convertBean2Str(param);
			CTWalletLog.printW("request--->"+params);
//			String result = executeHttpsPost(host, params, true);
//			System.out.println("result---->"+result);
			InputStream inStream = executeHttpsPost(host, params);
			Outer outer = Converter.getBean(outerClazz, insides, inStream);
			CTWalletLog.printW("response--->" + outer.toString());
			return outer;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (e.getClass().equals(FileNotFoundException.class)) {
				status = WebServiceConstant.EXCEPTION_SERVER_ERROR;
			} else if (e.getClass().equals(ConnectException.class)) {
				status = WebServiceConstant.EXCEPTION_CONNECT_FAILED;
			} else {
				status = WebServiceConstant.EXCEPTION_TIME_OUT;
			}
		}
		return status;
	}
	
	
	
}
